package com.team.lostProperty.controller;

import com.team.lostProperty.entity.*;
import com.team.lostProperty.service.NoticeService;
import com.team.lostProperty.utils.IdWorker;
import com.team.lostProperty.utils.TimeUtil;
import com.team.lostProperty.utils.UploadUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    private IdWorker idWorker=new IdWorker(1,1,1);

    @Resource
    private NoticeService noticeService;

    Result result = new Result();

    @CrossOrigin
    @RequestMapping("/selectAll")
    public Result selectAll(@RequestParam(required = false, defaultValue = "0") int requestType) {
        List<Notice> list=noticeService.selectAll();
        Result result = new Result();
        if(requestType==0){
            result.setData(list);
        }else{
            result.setDataJson(list);
        }
        result.setCount(list.size());
        result.setCode(0);
        result.setMsg("查询成功");
        return result;
    }

    /**
     * 根据条件查询公告信息
     * @param request
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/selectNotice")
    private Result selectPushInfo(HttpServletRequest request){
        Notice notice = new Notice();
        Admin admin = new Admin();
        if (!request.getParameter("id").equals("")){
            notice.setNoticeId(Long.valueOf(request.getParameter("id")));
        }
        admin.setNickName(request.getParameter("author"));
        notice.setAdmin(admin);
        notice.setTitle(request.getParameter("title"));

        List<Notice> noticeList = noticeService.selectNotice(notice);
        if (noticeList.size() >= 0){
            result.setCode(0);
            result.setMsg("成功");
            result.setData(noticeList);
            result.setCount(noticeList.size());
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
            result.setCount(0);
        }
        return result;
    }

    /**
     * 根据ID删除信息
     * @param ids
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/deleteNotice")
    private Result deletePushInfo(String ids){
        int i = 0;
        if (ids != null && !ids.equals("")) {
            String[] str = ids.split(",");
            for (String id : str) {
                if (id != null && !id.equals("")) {
                    i = noticeService.deleteNotice(Long.valueOf(id));
                }
            }
        }
        if (i > 0){
            result.setCode(0);
            result.setMsg("成功");
            result.setData("");
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
        }
        return result;
    }

    @CrossOrigin
    @RequestMapping("/imageUpload")
    public Result imageUpload(MultipartFile file, HttpServletRequest request)  {
        Result result= UploadUtil.fileSave(file,request,"notice");
        return result;

    }

    @CrossOrigin
    @RequestMapping("/insert")
    public Result insert(String title,String content) {
        Notice notice=new Notice();
        notice.setNoticeId(idWorker.nextId());
        notice.setTitle(title);
        notice.setContent(content);
        notice.setCreatedDate(TimeUtil.getNowDetailedTime());
        Admin admin=new Admin();
        admin.setAdminId(1);
        notice.setAdmin(admin);
        Result result = new Result();
        try {
            noticeService.insertNotice(notice);
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("title", title);
            parm.put("msg",content);
//            JPushUtils.jpushAndroid(parm);
            result.setData("success");
            result.setCount(1);
            result.setCode(Result.RESULT_OK);
            result.setMsg("公告发布成功");
            return result;
        }catch (Exception e){
            result.setData("error");
            result.setCount(0);
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("公告发布失败");
            return result;
        }

    }
}
